'use strict';

const _ = require('lodash');
const path = require('path');
const {exec} = require('child_process');
const {Controller} = require('ee-core');
const {
    app: electronApp, dialog, shell, BrowserView, session, Notification, sessionpowerMonitor, screen, nativeTheme
} = require('electron');
const Ps = require('ee-core/ps');
const Log = require('ee-core/log');
const dayjs = require('dayjs');
const axios = require('axios')
const fs = require('fs');

/**
 * Requests控制器
 * @class
 */
class RequestsController extends Controller {

    constructor(ctx) {
        super(ctx);
    }

    async get(arg) {
        const {url, headers = {}} = arg;
        const headersCurrency = {
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
        };
        try {
            const response = await axios.get(url, {headers: Object.assign(headersCurrency, headers), timeout: 30000});
            return response.data;
        } catch (error) {
            // console.error(error.response);
            return error.response.data.error;  // 返回一个 Error 对象
        }
    }


    async post(arg) {
        const {url, headers = {}, data} = arg
        const headersCurrency = {
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
        };
        try {
            const response = await axios.post(url, data, {
                headers: Object.assign(headersCurrency, headers),
                timeout: 30000
            });
            return response.data;
        } catch (error) {
            console.error(error.response.data.error);
            return error.response.data.error; // 返回一个 Error 对象
        }
    }


    async blob(arg) {
        const {url, headers = {}} = arg
        const headersCurrency = {
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
        };
        try {
            const response = await axios.get(url, {
                headers: Object.assign(headersCurrency, headers),
                timeout: 30000,
                responseType: "blob",
            });
            return response.data;
        } catch (error) {
            console.error(error);
            throw new Error('请求出错'); // 返回一个 Error 对象
        }
    }


    async downLoad(arg) {
        /**
         * dir_path 保存路径
         * */
        const {url, headers = {}, dir_path} = arg
        if (!dir_path) {
            return {errCode: 401, msg: "路径不能为空"}
        }
        let headerscurrency = {
            'Content-Type': 'multipart/form-data',
            "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"
        }
        return await axios.get(url, {
            headers: {...headerscurrency, ...headers},
            responseType: 'arraybuffer'
        }).then(res => {
            fs.promises.writeFile(dir_path, res.data, 'binary')
            return {errCode: 401, msg: "下载成功"}
        }).catch(err => {
            throw new Error('请求出错');
        })

    }


}

RequestsController.toString = () => '[class RequestsController]';
module.exports = RequestsController;
